﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using System.IO;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Kinect;
using Kinect_Dance.Objects;

namespace Kinect_Dance.Objects
{
    public class C_ManageCircle
    {
        public List<C_pCircle> ListCircle { get; set; }

        public int score { get; set; }

        public C_ManageCircle()
        {
            ListCircle = new List<C_pCircle>();
        }

        public void LoadFile(string idsong)
        {
            string line;
            StreamReader streamReader = new StreamReader(idsong + ".txt");
            while ((line = streamReader.ReadLine())!=null)
            {
                //0000_01 550 320_02 55 33
                
                string [] arraystring = line.Split('_');

                for (int i = 1; i < arraystring.Length; i++ )
                {

                    C_pCircle newCircle = new C_pCircle(int.Parse(arraystring[0]));
                    if (i !=0)
                    {
                        newCircle.timeStart = int.Parse(arraystring[0]);

                        string[] array = arraystring[i].Split(' ');
                        newCircle.ToadoX = int.Parse(array[1]);
                        newCircle.ToadoY = int.Parse(array[2]);
                        switch (int.Parse(array[0]))
                        {
                            case (int)Type.Head:
                                newCircle.type =  JointType.Head;
                                break;
                            case (int)Type.HandRigh:
                                newCircle.type =JointType.HandRight;
                                break;
                            case (int)Type.HandLeft:
                                newCircle.type =JointType.HandLeft;
                                break;
                            case (int)Type.FootRight:
                                newCircle.type =JointType.FootRight;
                                break;
                            case (int)Type.FootLeft:
                                newCircle.type = JointType.FootLeft;
                                break;            
            
                        
                        }
                        for (int j = 0; j < 2; j++)
                        {
                            newCircle.ParamInt[j] = int.Parse(array[j]);
                        }
                        ListCircle.Add(newCircle);
                    }
                   
                    
                }
            }
        }



        public void draw(GameTime gameTime,SpriteBatch spritebatch)
        {
            foreach (C_pCircle mCircle in ListCircle)
            {
                if (mCircle.timeStart < (int)gameTime.TotalGameTime.TotalMilliseconds)
                {
                    if ((double)(mCircle.timeLife + mCircle.timeStart) > gameTime.TotalGameTime.TotalMilliseconds)
                    {
                        mCircle.f_draw(spritebatch);
                    }   
                }
            }
        }

        public void update(GameTime gameTime, Skeleton[] skeletondata)
        {
            foreach (C_pCircle mCircle in ListCircle)
            {
                if (mCircle.timeStart < (int)gameTime.TotalGameTime.TotalMilliseconds)
                {
                    if ((double)(mCircle.timeLife + mCircle.timeStart) > gameTime.TotalGameTime.TotalMilliseconds)
                    {
                        score += mCircle.f_TargetNode(skeletondata);
                    }
                }

            }

            for (int i = 0; i < ListCircle.Count; i++)
            {
                if ((double)(ListCircle[i].timeLife + ListCircle[i].timeStart) < gameTime.TotalGameTime.TotalMilliseconds)
                {
                    ListCircle.Remove(ListCircle[i]);
                }
          
            }
        }
    }
}
